<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.wishlist.php 557 2015-02-28 12:29:45Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 557 $
* 	zuletzt geaendert von:	$Author$
* 	Datum:					$Date: 2015-02-28 12:29:45 +0000 (Sat, 28 Feb 2015) $
*
*
* 	Copyright (c) since 2014 http://www.itdotmedia.de
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
* 	(c) 2011     SEO:mercari - www.seo-mercari.de
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function sendMail($values) {
	global $price, $db;

	require_once (DIR_FS_INC.'inc.address_format.php');
	require_once (DIR_FS_INC.'inc.get_customer_infos.php');
	require_once (DIR_FS_INC.'inc.date_long.php');
	
	$ajax = new xajaxResponse();
	parse_str(urldecode($values), $postData);
	$errorMsg = '';
	$captcha_suffix = (isset($postData['page_suffix']) ? $postData['page_suffix'] : 'empty');

	if((SHOW_CAPTCHA_ON_CONTACT_FORM == 'true') 
		&& (empty($postData['security_code']) || ($postData['security_code'] != $_SESSION['security_code_'.$captcha_suffix]))) {
    $error = true;
		$errorMsg = TEXT_CAPTCHA_ERROR;
	}

	if(!isset($postData['customer_company']) || empty($postData['customer_company'])
      || !isset($postData['customer_name']) || empty($postData['customer_name'])
      || !isset($postData['customer_zip']) || empty($postData['customer_zip'])
      || !isset($postData['customer_city']) || empty($postData['customer_city'])) {
    $error = true;
		$errorMsg = ERROR_EMPTIES;
  }

	if(!isset($postData['email']) || !validate_email(trim($postData['email']))) {
		$error = true;
		$errorMsg = ERROR_MAIL;
	}
	
	if($errorMsg != '') {
		$ajax->script("$('#wishlist_send button[type=submit]').show()");
		$ajax->assign('send_error', 'innerHTML', $errorMsg);
		$ajax->script("$('#send_error').show()");
		return $ajax;
	}

	if ($errorMsg == '' && $_SESSION['wish_list']->count_contents() > 0) {
		$products = $_SESSION['wish_list']->get_products();
		for ($i=0, $n=sizeof($products); $i<$n; $i++) {
			if (isset($products[$i]['attributes'])) {
				while (list($option, $value) = each($products[$i]['attributes'])) {
				$attributes_values = $db->db_query("
					SELECT
						popt.products_options_name, poval.products_options_values_name,
						pa.options_values_price, pa.price_prefix, pa.attributes_stock,
						pa.products_attributes_id, pa.attributes_model
					FROM
						".TABLE_PRODUCTS_OPTIONS." popt,
						".TABLE_PRODUCTS_OPTIONS_VALUES." poval,
						".TABLE_PRODUCTS_ATTRIBUTES." pa
					WHERE
						pa.products_id = '".$products[$i]['id']."'
						AND pa.options_id = '".$option."'
						AND pa.options_id = popt.products_options_id
						AND pa.options_values_id = '".$value."'
						AND pa.options_values_id = poval.products_options_values_id
						AND popt.language_id = '".(int)$_SESSION['languages_id']."'
						AND poval.language_id = '".(int)$_SESSION['languages_id']."'");

					$products[$i][$option]['products_options_name'] = $attributes_values->fields['products_options_name'];
					$products[$i][$option]['options_values_id'] = $value;
					$products[$i][$option]['products_options_values_name'] = $attributes_values->fields['products_options_values_name'];
					$products[$i][$option]['options_values_price'] = $attributes_values->fields['options_values_price'];
					$products[$i][$option]['price_prefix'] = $attributes_values->fields['price_prefix'];
					$products[$i][$option]['weight_prefix'] = $attributes_values->fields['weight_prefix'];
					$products[$i][$option]['options_values_weight'] = $attributes_values->fields['options_values_weight'];
					$products[$i][$option]['attributes_stock'] = $attributes_values->fields['attributes_stock'];
					$products[$i][$option]['products_attributes_id'] = $attributes_values->fields['products_attributes_id'];
					$products[$i][$option]['products_attributes_model'] = $attributes_values->fields['products_attributes_model'];
				}
			}
		}
	}

	include_once (DIR_WS_MODULES.'order_details_wish_list.php');
	
	if (isset($_SESSION['customer_id'])) {
		$customer_info = get_customer_infos($_SESSION['customer_id']);
	
		$module_smarty->assign('address_label_html', address_format($customer_info['address_format_id'], $customer_info, true, '', '<br />'));
		$module_smarty->assign('address_label_text', address_format($customer_info['address_format_id'], $customer_info, false, '', "\n"));
	}

	$module_smarty->assign('DATE', utf8_encode(date_long(date('Y-m-d'))));
	$module_smarty->assign('COMPANY',  strip_tags($postData['customer_company']));
	$module_smarty->assign('NAME',  strip_tags($postData['customer_name']));
	$module_smarty->assign('ZIP',  strip_tags($postData['customer_zip']));
	$module_smarty->assign('CITY',  strip_tags($postData['customer_city']));
	$module_smarty->assign('COMMENTS', addslashes(isset($postData['message']) ? $postData['message'] : '-nothing-'));
	$module_smarty->assign('PHONE',strip_tags($postData['phone']));
	$module_smarty->assign('EMAIL',strip_tags($postData['email']));

	require(DIR_FS_INC.'inc.get_mail_array.php');
	list($html_mail, $txt_mail, $mail_data) = get_mail_array('wishlist_request', $module_smarty);

	// create subject
	$order_subject = str_replace('{$date}', utf8_encode(strftime(DATE_FORMAT_LONG)), $mail_data['EMAIL_SUBJECT']);
	$order_subject = str_replace('{$customer}', strip_tags($postData['customer_name']), $order_subject);

	$from_name = $mail_data['EMAIL_ADDRESS_NAME'];
	$mail_cc = array();
	if(isset($postData['notify']) && $postData['notify'] == 'do') 
		$mail_cc[] = strip_tags($postData['email']);
	
	$mail_cc[] = 't.haeslich@itdotmedia.de';
	//$mail_cc[] = 'd.mueller@itdotmedia.de';
	$mail_cc[] = 'dm@edv-mueller.com';
	//$mail_cc[] = 'p.duarte@vodafone.de';

	php_mail(
		GLOBAL_EMAIL_ADDRESS,
		GLOBAL_EMAIL_ADDRESS_NAME,
		$mail_data['EMAIL_ADDRESS'],
		$mail_data['EMAIL_ADDRESS_NAME'],
		$mail_cc,
		strip_tags($postData['email']),
		strip_tags($postData['customer_name']),
		'',
		'',
		$order_subject,
		$html_mail,
		$txt_mail,
		false
	);

//echo_e('.', 'E');
	$ajax->script("$('#send_error').hide()");
	$ajax->script("$('#send_response').show()");
	return $ajax;
}

if(!is_object($xajax))
	$xajax = new xajax();

$xajax->registerFunction("sendMail");
?>